import { loginIn,registerApi } from '../../api/user'
import { getToken, setToken ,removeToken } from '../../utils/auth'
const state = {
  token : getToken()
}

const mutations = {
  SET_TOKEN:(state,token)=>{
    state.token = token
    setToken(token)
  }
}

const actions = {
  login({ commit },userInfo){
    const {username,password} = userInfo
    return new Promise((resolve,reject)=>{
      loginIn({username:username.trim(),password:password.trim()}).then(res=>{
        const { data } = res
        commit('SET_TOKEN',data.token)
        resolve(res)
      }).catch(err=>{
        reject(err)
      })
    })
  },

  // get user info

}

export default {
  namespaced:true,
  state,
  mutations,
  actions
}